<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind" label-width="120px">
    
      <uni-forms-item name="customer_id" label="客户" required>
       <uni-data-select
       	 collection="mydb-customer"
       	 field="_id as value, name as text"
       	 v-model="formData.customer_id"
        />
      </uni-forms-item>
	  <uni-forms-item name="type" label="提醒类型" required>
	    <uni-data-checkbox v-model="formData.type" :localdata="formOptions.type_localdata"></uni-data-checkbox>
	  </uni-forms-item>
      <uni-forms-item name="user_id" label="负责人" required>
      <uni-data-select
      		 collection="uni-id-users"
      		 field="_id as value, nickname as text"
      		 v-mode="formData.user_id"
       />
      </uni-forms-item>
      <uni-forms-item name="trigger_time" label="提醒时间" required>
        <uni-datetime-picker v-model="formData.trigger_time"></uni-datetime-picker>
      </uni-forms-item>
	  <uni-forms-item name="title" label="提醒主题" required>
	    <uni-easyinput v-model="formData.title"></uni-easyinput>
	  </uni-forms-item>
	  <uni-forms-item name="content" label="提醒内容" required>
	    <uni-easyinput v-model="formData.content"></uni-easyinput>
	  </uni-forms-item>
      <uni-forms-item name="is_completed" label="状态">
       <uni-data-checkbox v-model="formData.is_completed" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/mydb-remind.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'mydb-remind';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "type": "",
        "customer_id": "",
        "user_id": "",
		"title":"",
		"content":"",
        "trigger_time": null,
        "is_completed": 0
      }
      return {
        formData,
        formOptions: {
          "type_localdata": [
            {
              "value": "follow",
              "text": "下次跟进"
            },
            {
              "value": "repayment",
              "text": "还款日"
            },
            {
              "value": "birthday",
              "text": "生日"
            }
          ],
		  "status_localdata": [
            {
              "value": 0,
              "text": "未提醒"
            },
            {
              "value": 1,
              "text": "已提醒"
            }
          ],
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
